﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Security.Permissions;
using System.Text;
using MvcContrib.UI.Pager;
using MvcContrib.Pagination;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace MvcContribJquery.Views.Home.Extensions
{
    [AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    public static class HtmlHelperExtensions
    {
        public static string ActionImage(this HtmlHelper html, string controller, string action, object routeValues, string imageRelativeUrl, string alt, object linkAttributes, object imageAttributes)
        {
            // use URL helper to find image path
            UrlHelper u = new UrlHelper(html.ViewContext.RequestContext);
            // use tag builder to manage image tag
            TagBuilder img = new TagBuilder("img");
            img.GenerateId(alt + "Img");
            img.MergeAttribute("src", imageRelativeUrl);
            img.MergeAttribute("alt", alt);
            img.MergeAttributes(new RouteValueDictionary(imageAttributes));

            TagBuilder t = new TagBuilder("a");
            //string url = RouteTable.Routes.GetVirtualPath(html.ViewContext.RequestContext, new RouteValueDictionary(routeValues)).VirtualPath;
            t.MergeAttribute("href", u.Action(action, controller, new RouteValueDictionary(routeValues)));
            t.InnerHtml = img.ToString(TagRenderMode.SelfClosing);
            t.MergeAttributes(new RouteValueDictionary(linkAttributes));
            return t.ToString(TagRenderMode.Normal);
        }
        public static string SubmitImage(this HtmlHelper htmlHelper, string imageLink, string value, string altText)
        {
            return string.Format("<input type='image' src='{0}' value='{1}' alt='{2}' />", imageLink, value, altText);
        }
        public static string PageDropDown(this HtmlHelper html, IPagination pagedata)
        {
            HttpRequestBase _request = html.ViewContext.HttpContext.Request;
            StringBuilder builder = new StringBuilder();
            builder.Append("<div class='pagedropdown'>");
            builder.Append("Page <select>");
            for (int i = 1; i <= pagedata.TotalPages; i++)
            {
                builder.AppendFormat("<option value=\"{0}\"{1}>{0}</option>", i, i == pagedata.PageNumber ? " selected=\"true\"" : "");
            }
            builder.AppendFormat(@"</select> of {0}", pagedata.TotalPages);
            builder.Append(@"</div>");
            return builder.ToString();
        }
    }
}
